﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using QuanLyGiaoVuDAL.DTO;
using QuanLyGiaoVuUI.Models.SinhVien;
using QuanLyGiaoVuUI.Models.BanTinSinhVien;
using QuanLyGiaoVuUI.Models.ChiTietThongBao;
using QuanLyGiaoVuUI.Models.ThongBao;
using QuanLyGiaoVuUI.Models.RssFeeds;
using QuanLyGiaoVuService.CommandServices;
using QuanLyGiaoVuService.ReportServices;
using QuanLyGiaoVuUI.Models.NguoiDung;
using QuanLyGiaoVuService.CommonServices;
using System.Runtime.CompilerServices;
using QuanLyGiaoVuUI.Models.GiangVien;
using QuanLyGiaoVuUI.Extensions.ExtendedAttributes;
using QuanLyGiaoVuUI.Extensions.Utilities;

namespace QuanLyGiaoVuUI.Controllers
{
    public class ThongBaoController : Controller
    {
        private IThongBaoCommandService _thongBaoCommandService;
        private IThongBaoReportService _thongBaoReportService;
        private IChiTietThongBaoCommandService _chiTietThongBaoCommandService;
        private IChiTietThongBaoReportService _chiTietThongBaoReportService;
        private INguoiDungReportService _nguoiDungReportService;
        private IGiangVienReportService _giangVienReportService;
        private ITaiLieuReportService _taiLieuReportService;

        public ThongBaoController(
            IThongBaoCommandService thongBaoCommandService,
            IThongBaoReportService thongBaoReportService,
            IChiTietThongBaoCommandService chiTietThongBaoCommandService,
            IChiTietThongBaoReportService chiTietThongBaoReportService,
            IGiangVienReportService giangVienReportService,
            ITaiLieuReportService taiLieuReportService
            )
        {
            _thongBaoCommandService = thongBaoCommandService;
            _thongBaoReportService = thongBaoReportService;
            _chiTietThongBaoCommandService = chiTietThongBaoCommandService;
            _chiTietThongBaoReportService = chiTietThongBaoReportService;
            _giangVienReportService = giangVienReportService;
            _taiLieuReportService = taiLieuReportService;
        }
        //
        // GET: /ThongBao/

        public ActionResult Index()
        {
            NguoiDungDTO myInfo = new NguoiDungDTO();
            if (Session["SinhVienInfo"] != null)
            {
                myInfo = (NguoiDungDTO)Session["SinhVienInfo"];
            }
            else
            {
                return Redirect("/SinhVien/DangNhap");
            }
            if (myInfo.VaiTro == 1) // =1 la giang vien; !=1 la sinh vien
            {
                return Redirect("/DangNhapClient/GiangVienDangNhap");
            }

            IEnumerable<ChiTietThongBaoDTO> chiTietThongBaoNguoiDung = _chiTietThongBaoReportService.GetChiTietThongBaoByNguoiNhan(myInfo.MaNguoiDung);
            IEnumerable<ChiTietThongBaoViewModel> chiTietThongBaoViewModel = ChiTietThongBaoViewModel.FromChiTietThongBaoDTOList(chiTietThongBaoNguoiDung);

            return View(chiTietThongBaoViewModel);
        }
        
        [HttpPost]
        public ActionResult GetSoLuongThongBaoChuaXem()
        {
            NguoiDungDTO myInfo = new NguoiDungDTO();
            if (Session["SinhVienInfo"] != null)
            {
                myInfo = (NguoiDungDTO)Session["SinhVienInfo"];
            }
            else
            {
                return Redirect("/SinhVien/DangNhap");
            }

            IEnumerable<ChiTietThongBaoDTO> chiTietThongBaoNguoiDung = _chiTietThongBaoReportService.GetChiTietThongBaoByNguoiNhan(myInfo.MaNguoiDung);
            int number = 0;
            if (chiTietThongBaoNguoiDung != null && chiTietThongBaoNguoiDung.Count() > 0)
            {
                number = (from p in chiTietThongBaoNguoiDung where (p.DaXem == 0) select p).Count();
                //foreach (var chitiet in chiTietThongBaoNguoiDung)
                //{
                //    if (chitiet.DaXem == 0)
                //    {
                //        number++;
                //    }
                //}
            }
            
            return Json(new { thongbaochuaxem = number });
        }
        
        public ActionResult ChiTiet(Guid id,Guid mct)
        {
            NguoiDungDTO myInfo = new NguoiDungDTO();
            if (Session["SinhVienInfo"] != null)
            {
                myInfo = (NguoiDungDTO)Session["SinhVienInfo"];
            }
            else
            {
                return Redirect("/SinhVien/DangNhap");
            }
            if (myInfo.VaiTro == 1) // =1 la giang vien; !=1 la sinh vien
            {
                return Redirect("/DangNhapClient/GiangVienDangNhap");
            }
            ThongBaoDTO thongbao = _thongBaoReportService.GetThongBaoById(id);
            ThongBaoViewModel thongBaoViewModel = ThongBaoViewModel.FromThongBaoDTO(thongbao);
            ChiTietThongBaoDTO chiTietThongBao = _chiTietThongBaoReportService.GetChiTietThongBaoById(mct);
            if (chiTietThongBao != null)
            {
                chiTietThongBao.DaXem = 1;
                try
                {
                    _chiTietThongBaoCommandService.UPdateChiTietThongBao(chiTietThongBao);
                }
                catch (Exception ex)
                {
                    return View(thongBaoViewModel);
                }
            }
            return View(thongBaoViewModel);
        }
        [HttpPost]
        public ActionResult DeleteThongBaoSinhVien(String listID)
        {
            bool isSuccess = true;
            String[] arrID = listID.Split(',');
            try
            {
                if (arrID.Length > 0)
                {
                    foreach (var id in arrID)
                    {
                        if (id != "")
                        {
                            isSuccess = _chiTietThongBaoCommandService.DeleteChiTietThongBao(Guid.Parse(id));
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                isSuccess = false;
            }

            return Json(new { success = isSuccess });
        }

        [HttpGet]
        [GiangVienAuthorize]
        public ActionResult ThongBaoGiangVien()
        {
            IEnumerable<ChiTietThongBaoDTO> chiTietThongBaoNguoiDung = _chiTietThongBaoReportService.GetChiTietThongBaoByNguoiNhan(UserUtility.GetCurrentUser().MaNguoiDung);
            IEnumerable<ChiTietThongBaoViewModel> chiTietThongBaoViewModel = ChiTietThongBaoViewModel.FromChiTietThongBaoDTOList(chiTietThongBaoNguoiDung);

            return View(chiTietThongBaoViewModel);
        }

        [HttpPost]
        [GiangVienAuthorize]
        public ActionResult GetSoLuongThongBaoGiangVienChuaXem()
        {
            IEnumerable<ChiTietThongBaoDTO> chiTietThongBaoNguoiDung = _chiTietThongBaoReportService.GetChiTietThongBaoByNguoiNhan(UserUtility.GetCurrentUser().MaNguoiDung);
            int number = 0;
            if (chiTietThongBaoNguoiDung != null && chiTietThongBaoNguoiDung.Count() > 0)
            {
                number = (from p in chiTietThongBaoNguoiDung where (p.DaXem == 0) select p).Count();
            }

            return Json(new { thongbaochuaxem = number });
        }

        [HttpGet]
        [GiangVienAuthorize]
        public ActionResult ChiTietThongBaoGiangVien(Guid id, Guid mct)
        {
            ThongBaoDTO thongbao = _thongBaoReportService.GetThongBaoById(id);
            ThongBaoViewModel thongBaoViewModel = ThongBaoViewModel.FromThongBaoDTO(thongbao);
            thongBaoViewModel.DanhSachTaiLieuDinhKem = new List<TaiLieuDTO>();
            if (thongBaoViewModel.MaTaiLieu1.HasValue && thongBaoViewModel.MaTaiLieu1 != Guid.Empty) thongBaoViewModel.DanhSachTaiLieuDinhKem.Add(_taiLieuReportService.GetTaiLieuById((Guid)thongBaoViewModel.MaTaiLieu1));
            if (thongBaoViewModel.MaTaiLieu2.HasValue && thongBaoViewModel.MaTaiLieu2 != Guid.Empty) thongBaoViewModel.DanhSachTaiLieuDinhKem.Add(_taiLieuReportService.GetTaiLieuById((Guid)thongBaoViewModel.MaTaiLieu2));
            if (thongBaoViewModel.MaTaiLieu3.HasValue && thongBaoViewModel.MaTaiLieu3 != Guid.Empty) thongBaoViewModel.DanhSachTaiLieuDinhKem.Add(_taiLieuReportService.GetTaiLieuById((Guid)thongBaoViewModel.MaTaiLieu3));
            if (thongBaoViewModel.MaTaiLieu4.HasValue && thongBaoViewModel.MaTaiLieu4 != Guid.Empty) thongBaoViewModel.DanhSachTaiLieuDinhKem.Add(_taiLieuReportService.GetTaiLieuById((Guid)thongBaoViewModel.MaTaiLieu4));
            if (thongBaoViewModel.MaTaiLieu5.HasValue && thongBaoViewModel.MaTaiLieu5 != Guid.Empty) thongBaoViewModel.DanhSachTaiLieuDinhKem.Add(_taiLieuReportService.GetTaiLieuById((Guid)thongBaoViewModel.MaTaiLieu5));
            

            ChiTietThongBaoDTO chiTietThongBao = _chiTietThongBaoReportService.GetChiTietThongBaoById(mct);
            if (chiTietThongBao != null)
            {
                chiTietThongBao.DaXem = 1;
                try
                {
                    _chiTietThongBaoCommandService.UPdateChiTietThongBao(chiTietThongBao);
                }
                catch (Exception ex)
                {
                    return View(thongBaoViewModel);
                }
            }
            return View(thongBaoViewModel);
        }

        [HttpPost]
        public ActionResult DeleteThongBaoGiangVien(String listID)
        {
            NguoiDungDTO myInfo = new NguoiDungDTO();
            if (Session["UserInfo"] != null)
            {
                myInfo = (NguoiDungDTO)Session["UserInfo"];
            }
            else
            {
                return Redirect("/DangNhapClient/GiangVienDangNhap");
            }
            if (myInfo.VaiTro != 1) // =1 la giang vien; !=1 la sinh vien
            {
                return Redirect("/SinhVien/Index");
            }

            bool isSuccess = true;
            String[] arrID = listID.Split(',');
            try
            {
                if (arrID.Length > 0)
                {
                    foreach (var id in arrID)
                    {
                        if (id != "")
                        {
                            isSuccess = _chiTietThongBaoCommandService.DeleteChiTietThongBao(Guid.Parse(id));
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                isSuccess = false;
            }

            return Json(new { success = isSuccess });
        }

        [HttpGet]
        [GiangVienAuthorize]
        public ActionResult ThongBaoDaGui() {
            IEnumerable<ThongBaoViewModel> danhSachThongBao = ThongBaoViewModel.FromThongBaoDTOList(_thongBaoReportService.GetAllThongBao(GetCurrentUser().MaNguoiDung));
            return View(danhSachThongBao);
        }

        [HttpGet]
        [GiangVienAuthorize]
        public ActionResult ThongBaoMoiGiangVien() {
            ThongBaoViewModel viewModel = new ThongBaoViewModel();
            return View("TaoThongBao",viewModel);
        }

        [HttpGet]
        [GiangVienAuthorize]
        public ActionResult DanhSachThongBaoDaGui() {
            IEnumerable<ThongBaoViewModel> danhSachThongBao = ThongBaoViewModel.FromThongBaoDTOList(_thongBaoReportService.GetAllThongBao(GetCurrentUser().MaNguoiDung));
            return View(danhSachThongBao);
        }

        #region Giao Vu
        [HttpGet]
        [GiangVienAuthorize(ChiGiaoVu=true)]
        public ActionResult ThongBaoGiaoVu()
        {
            IEnumerable<ChiTietThongBaoDTO> chiTietThongBaoNguoiDung = _chiTietThongBaoReportService.GetChiTietThongBaoByNguoiNhan(UserUtility.GetCurrentUser().MaNguoiDung);
            IEnumerable<ChiTietThongBaoViewModel> chiTietThongBaoViewModel = ChiTietThongBaoViewModel.FromChiTietThongBaoDTOList(chiTietThongBaoNguoiDung);

            return View(chiTietThongBaoViewModel);
        }

        [HttpGet]
        [GiangVienAuthorize(ChiGiaoVu=true)]
        public ActionResult ThongBaoGiaoVuDaGui()
        {
                IEnumerable<ThongBaoViewModel> danhSachThongBao = ThongBaoViewModel.FromThongBaoDTOList(_thongBaoReportService.GetAllThongBao(GetCurrentUser().MaNguoiDung));
                return View(danhSachThongBao);
        }

        [HttpGet]
        [GiangVienAuthorize(ChiGiaoVu=true)]
        public ActionResult TaoThongBao()
        {
            ThongBaoViewModel viewModel = new ThongBaoViewModel();
            return View(viewModel);
        }

        [HttpPost]
        [GiangVienAuthorize]
        public ActionResult GuiThongBao(ThongBaoViewModel viewModel)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var thongBaoDTo = viewModel.ToThongBaoDTO();
                    thongBaoDTo.NgayGuiThongBao = DateTime.Now;
                    thongBaoDTo.NguoiGuiId = UserUtility.GetCurrentUser().MaNguoiDung;
                    _thongBaoCommandService.InsertThongBao(thongBaoDTo, viewModel.NguoiNhanIDs);

                    if (viewModel.GuiQuaEmail == true)
                    {
                        var dsEmailNguoiNhan = _giangVienReportService.GetEmailListByGiangVienIdList(viewModel.NguoiNhanIDs);
                        EmailService.GetInstance().SendEmail(new QuanLyGiaoVuService.Domain.EmailModel
                        {
                            TieuDe = viewModel.TieuDeThongBao,
                            NoiDung = viewModel.NoiDungThongBao,
                            DiaChiNguoiNhan = dsEmailNguoiNhan
                        });
                    }
                    return Json(new
                    {
                        success = true
                    });
                }
                else
                    return Json(new { 
                        success = false,
                        message = "Chưa nhập đủ thông tin."
                    });
            }
            catch (Exception exception)
            {
                return Json(new { 
                    success = false,
                    message = exception.Message
                });
            }
        }

        [HttpGet]
        [GiangVienAuthorize]
        public ActionResult ChonNguoiNhan()
        {
            IEnumerable<GiangVienViewModel> viewModel = GiangVienViewModel.FromGiangVienDTOList(_giangVienReportService.GetAllGiangVien());
            return View(viewModel);
        }
        #endregion
        [HttpGet]
        [GiangVienAuthorize(ChiGiaoVu = true)]
        public ActionResult ChiTietThongBaoGiaoVu(Guid id, Guid mct)
        {
            ThongBaoDTO thongbao = _thongBaoReportService.GetThongBaoById(id);
            ThongBaoViewModel thongBaoViewModel = ThongBaoViewModel.FromThongBaoDTO(thongbao);
            thongBaoViewModel.DanhSachTaiLieuDinhKem = new List<TaiLieuDTO>();
            if (thongBaoViewModel.MaTaiLieu1.HasValue && thongBaoViewModel.MaTaiLieu1 != Guid.Empty) thongBaoViewModel.DanhSachTaiLieuDinhKem.Add(_taiLieuReportService.GetTaiLieuById((Guid)thongBaoViewModel.MaTaiLieu1));
            if (thongBaoViewModel.MaTaiLieu2.HasValue && thongBaoViewModel.MaTaiLieu2 != Guid.Empty) thongBaoViewModel.DanhSachTaiLieuDinhKem.Add(_taiLieuReportService.GetTaiLieuById((Guid)thongBaoViewModel.MaTaiLieu2));
            if (thongBaoViewModel.MaTaiLieu3.HasValue && thongBaoViewModel.MaTaiLieu3 != Guid.Empty) thongBaoViewModel.DanhSachTaiLieuDinhKem.Add(_taiLieuReportService.GetTaiLieuById((Guid)thongBaoViewModel.MaTaiLieu3));
            if (thongBaoViewModel.MaTaiLieu4.HasValue && thongBaoViewModel.MaTaiLieu4 != Guid.Empty) thongBaoViewModel.DanhSachTaiLieuDinhKem.Add(_taiLieuReportService.GetTaiLieuById((Guid)thongBaoViewModel.MaTaiLieu4));
            if (thongBaoViewModel.MaTaiLieu5.HasValue && thongBaoViewModel.MaTaiLieu5 != Guid.Empty) thongBaoViewModel.DanhSachTaiLieuDinhKem.Add(_taiLieuReportService.GetTaiLieuById((Guid)thongBaoViewModel.MaTaiLieu5));


            ChiTietThongBaoDTO chiTietThongBao = _chiTietThongBaoReportService.GetChiTietThongBaoById(mct);
            if (chiTietThongBao != null)
            {
                chiTietThongBao.DaXem = 1;
                try
                {
                    _chiTietThongBaoCommandService.UPdateChiTietThongBao(chiTietThongBao);
                }
                catch (Exception ex)
                {
                    return View(thongBaoViewModel);
                }
            }
            return View(thongBaoViewModel);
        }
        [HttpPost]
        [GiangVienAuthorize(ChiGiaoVu = true)]
        public ActionResult DeleteThongBaoGiaoVu(String listID)
        {            
            bool isSuccess = true;
            String[] arrID = listID.Split(',');
            try
            {
                if (arrID.Length > 0)
                {
                    foreach (var id in arrID)
                    {
                        if (id != "")
                        {
                            isSuccess = _chiTietThongBaoCommandService.DeleteChiTietThongBao(Guid.Parse(id));
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                isSuccess = false;
            }
            return Json(new { success = isSuccess });
        }
        #region Private methods
        private NguoiDungDTO GetCurrentUser()
        {
            NguoiDungDTO myInfo = new NguoiDungDTO();
            if (Session["UserInfo"] != null)
            {
                myInfo = (NguoiDungDTO)Session["UserInfo"];
            }
            else
                myInfo = null;
            return myInfo;
        }
        #endregion
    }
}
